package com.david.func_test

import android.content.Context
import android.os.*


object VibrateUtils {
    fun vibrateOnce(context: Context, time:Long){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val manager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
            manager.vibrate(CombinedVibration.createParallel(VibrationEffect.createOneShot(time, -1)))


//            manager.vibrate(CombinedVibration.createParallel(VibrationEffect.createWaveform(
//                longArrayOf(3000, 1000, 500, 2000), -1)))

//            manager.cancel()
        } else {
            val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
            vibrator.vibrate(time)

//            val patter = longArrayOf(1000, 1000, 2000, 50)
//        vibrator.vibrate(patter, -1)

//            vibrator.cancel()

            //用户按住震动，抬起停止可设置震动时间1分钟，用户手指抬起时取消震动
        }
    }

    fun startVibrate(context: Context, longArrayOf: LongArray){
//        val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
//        vibrator.vibrate(1000)

//        val patter = longArrayOf(1000, 1000, 2000, 50)
//        vibrator.vibrate(patter, -1)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val manager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
//            manager.vibrate(CombinedVibration.createParallel(VibrationEffect.createOneShot(1000, -1)))


            manager.vibrate(CombinedVibration.createParallel(VibrationEffect.createWaveform(
                longArrayOf, -1)))

//            manager.cancel()
        } else {
            val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
            vibrator.vibrate(1000)

//            val patter = longArrayOf(1000, 1000, 2000, 50)
//        vibrator.vibrate(patter, -1)

//            vibrator.cancel()

            //用户按住震动，抬起停止可设置震动时间1分钟，用户手指抬起时取消震动
        }
    }
}